import sys
import json
import ibm_db
import datetime
import pymysql
#导出规则的更新语句 用于提炼报警算子的报警内容
class TestWork:
    def __init__(self, data: dict = None):
        # self.type = data['type']
        # self.host = data['host']
        # self.port = data['port']
        # self.user = data['user']
        # self.password = data['password']
        # self.database = data['database']
        # self.schema = data['schema']
        # self.table = data['table']
        self.type = "db2"
        self.host = "172.26.34.9"#炼钢
        # self.host = "172.26.2.9"  # 炼铁
        self.port = "13306"
        self.user = "root"
        self.password = "mysql"
        self.database = "iplatdb"
        self.schema = "IPLATTEST"
        self.table = "ALARM_DATABASE_OPEARATOR"

    def db_connect(self):
        self.db = pymysql.connect(host=self.host,
                                  user=self.user,
                                  passwd=self.password,
                                  port=int(self.port),
                                  database=self.database,
                                  charset='utf8')

    def work(self):
        self.db_connect();
        conn =self.db;
        querySql="""select rule_id,
            rule_name_en,
            rule_name_ch,
            alg_id,
            rule_type,
            rule_description,
            rule_remark,
            rule_params,
            rule_graph,
            rule_event,
            rule_enable,
            rule_creator,
            rule_createtime,
            rule_updatetime,
            rule_tree_Id from tbipb20 where rule_description =''"""#规则表
        # 得到一个可以执行SQL语句的光标对象
        cursor = conn.cursor()
        # 定义要执行的SQL语句
        sql=querySql
        # 执行SQL语句
        ret=cursor.execute(sql)
        results=cursor.fetchall()
        updateSqlList=[]
        for result in results:
            try:
                warnidList=[]
                sourceIdList=[]
                rule_params=json.loads(result[7])
                rule_graph=json.loads(result[8])
                for node in rule_graph["nodes"]:
                    if node["metadata"]["value"]=="kungangLTWarningOPREA"or node["metadata"]["value"]=="kungangLGWarningOPREA":
                        warnidList.append(node["id"])
                for edge in rule_graph["edges"]:
                    if warnidList.__contains__(edge["target"]) and not edge["condition"]=="Yes" :
                        sourceIdList.append(edge["source"])
                desc=""
                for sourceId in sourceIdList:
                    desc=desc+rule_params[sourceId]["config"]["constant"]+";"
                # print("warnidList is ",warnidList)
                # print("sourceidList is ",sourceIdList)
                # print("desc is ",desc)
                # print(result[8])
                updateSqlList.append("update tbipb20 set rule_description=\""+desc+"\" where rule_id=\""+result[0]+"\"")
            except Exception as e:
                pass
            # return
        # 关闭光标对象
        cursor.close()
        # 关闭数据库连接
        conn.close()
        print("")
        for sql in updateSqlList:
            print(sql+";")
if __name__ == '__main__':
    TestWork().work()